﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FreedomERP.Dao.Bd;
using FreedomERP.Dao.Bd.Impl;
using FreedomERP.Models;
using System.Collections;
using FreedomERP.Comm;
using FreedomERP.Services;
using FreedomERP.Services.Bd;

namespace FreedomERP.Services.Bd.Impl
{
    public class BdCategoryServiceImpl : IBdCategoryService
    {
        IBdCategoryDao categoryDao = new BdCategoryDaoImpl();

        /// <summary>
        /// 查找龙彩的产品规格信息
        /// </summary>
        /// <param name="fName">规格名</param>
        /// <param name="fRemark">规格备注</param>
        /// <returns></returns>
        public List<BdCategory> findModelCategories(String fName, String fRemark, int start, int limit, out int totalCount, string currAccountOrgunitFId)
        {
            IBdOrgunitDao orgDao = new BdOrgunitDaoImpl();
            //DAOResult<String> orgDaoResult = orgDao.findOrgunitId(ConstData.ORGUNIT_NAME_LC);
            //String orgnuitId = null;
            DAOResult<BdCategory> cateDaoResult = null;
            //if (orgDaoResult.result && (orgnuitId = orgDaoResult.resultSet.First<String>()) != null)
            //{
            cateDaoResult = categoryDao.findBdCategories(null, currAccountOrgunitFId, ConstData.CATEGORY_PROD_MODEL_TYPE, fName, null, fRemark, ConstData.CATEGORY_STATUS_YES, true, start, limit, out totalCount);
            if (cateDaoResult.result)
            {
                return cateDaoResult.resultSet;
            }
            else
            {
                throw new Exception(cateDaoResult.msg);
            }
            //}
            //else
            //{
            //    throw new Exception("商户信息不存在");
            //}
        }

        /// <summary>
        /// 查找龙彩的订单状态列表
        /// </summary>
        /// <param name="fName">规格名</param>
        /// <returns></returns>
        public List<BdCategory> findInvShortageOrderStatusCategories(String fName, int start, int limit, out int totalCount)
        {
            IBdOrgunitDao orgDao = new BdOrgunitDaoImpl();
            DAOResult<String> orgDaoResult = orgDao.findOrgunitId(ConstData.ORGUNIT_NAME_LC);
            String orgnuitId = null;
            DAOResult<BdCategory> cateDaoResult = null;
            if (orgDaoResult.result && (orgnuitId = orgDaoResult.resultSet.First<String>()) != null)
            {
                cateDaoResult = categoryDao.findBdCategories(null, orgnuitId, ConstData.CATEGORY_INV_SHORTAGE_STATUS_TYPE, fName, null, null,null, true, start, limit, out totalCount);
                if (cateDaoResult.result)
                {
                    return cateDaoResult.resultSet;
                }
                else
                {
                    throw new Exception(cateDaoResult.msg);
                }
            }
            else
            {
                throw new Exception("商户信息不存在");
            }
        }

        /// <summary>
        /// 根据Id查找有效的龙彩的产品规格信息
        /// </summary>
        /// <param name="fId"></param>
        /// <returns></returns>
        public BdCategory findModelCategoryById(String fId)
        {
            //IBdOrgunitDao orgDao = new BdOrgunitDaoImpl();
            //DAOResult<String> orgDaoResult = orgDao.findOrgunitId(ConstData.ORGUNIT_NAME_LC);
            //String orgnuitId = null;
            DAOResult<BdCategory> cateDaoResult = null;
            //if (orgDaoResult.result && (orgnuitId = orgDaoResult.resultSet.First<String>()) != null)
            //{
                int totalCount;
                cateDaoResult = categoryDao.findBdCategories(fId, null, ConstData.CATEGORY_PROD_MODEL_TYPE, null, null, null,ConstData.CATEGORY_STATUS_YES, true, 0, 1, out totalCount);
                if (cateDaoResult.result)
                {
                    return cateDaoResult.resultSet.First<BdCategory>();
                }
                else
                {
                    throw new Exception(cateDaoResult.msg);
                }
            //}
            //else
            //{
            //    throw new Exception("商户信息不存在");
            //}
        }

        /// <summary>
        /// 新增或更改龙彩的产品规格信息
        /// </summary>
        /// <param name="fid"></param>
        /// <param name="fName"></param>
        /// <param name="fRemark"></param>
        public void saveOrUpdateModelCategory(String accountId, string currAccountOrgunitFId, String fId, String fName, String fRemark, out String returnFID)
        {
            returnFID = null;
            BdCategory category = new BdCategory();
            //IBdOrgunitDao orgDao = new BdOrgunitDaoImpl();
            //DAOResult<String> orgDaoResult = orgDao.findOrgunitId(ConstData.ORGUNIT_NAME_LC);
            //String orgnuitId = null;
            //if (orgDaoResult.result && (orgnuitId = orgDaoResult.resultSet.First<String>()) != null)
            //{
            category.FOrgunitId = currAccountOrgunitFId;
            //}
            //else
            //{
            //    throw new Exception("商户信息不存在，无法保存。");
            //}

            if (fId == null || ConstData.EMPTY_STRING == fId)
            {
                fId = null;
                category.FCreatorId = accountId;
                category.FCreateTime = DateTime.Now;
                category.FModifierId = accountId;
                category.FModifyTime = category.FCreateTime;
            }
            else
            {
                category.FId = fId;
                category.FModifierId = accountId;
                category.FModifyTime = DateTime.Now;
            }
            category.FName = fName;
            category.FNumber = fName;
            category.FRemark = fRemark;
            category.FType = ConstData.CATEGORY_PROD_MODEL_TYPE;
            DAOResult<String> daoResult = categoryDao.saveOrUpdateBdCategory(category);
            if (!daoResult.result)
            {
                throw new Exception(daoResult.msg);
            }
            if (daoResult.resultSet.Count > 0)
            {
                returnFID = daoResult.resultSet.First();
            }

        }

        /// <summary>
        /// 删除龙彩的产品规格信息
        /// </summary>
        /// <param name="fid"></param>
        public void deleteModelCategory(String fId)
        {
            DAOResult<BdCategory> daoResult = categoryDao.deleteProdCategory(fId);
            if (!daoResult.result)
            {
                throw new Exception(daoResult.msg);
            }
        }

        /// <summary>
        /// 根据FType获取Category列表
        /// </summary>
        /// <param name="FType"></param>
        /// <author>梁建东</author>
        /// <returns></returns>
        public List<BdCategory> QueryCategoryListByFType(String FType)
        {
            return categoryDao.QueryCategoryListByFType(FType);
        }

        /// <summary>
        /// 根据FId获取资料分类
        /// </summary>
        /// <param name="FId"></param>
        /// <author>姚少鹏</author>
        /// <returns></returns>
        public BdCategory getCategoryByFId(string FId)
        {
            return categoryDao.getCategoryByFId(FId);
        }

        /// <summary>
        /// 根据FName和FType获取资料分类
        /// </summary>
        /// <param name="FName">状态名称</param>
        /// <param name="FType">状态类型</param>
        /// <author>姚少鹏</author>
        /// <returns></returns>
        public BdCategory getCategory(string FName, string FType)
        {
            return categoryDao.getCategory(FName, FType);
        }

        /// <summary>
        /// 根据FNumber和FType获取资料分类
        /// </summary>
        /// <param name="FName">状态编号</param>
        /// <param name="FType">状态类型</param>
        /// <author>梁建东</author>
        /// <returns></returns>
        public BdCategory GetCategoryByFNumberAndFType(string FNumber, string FType)
        {
            BdCategory category = null;

            int totalCount;
            DAOResult<BdCategory> cateDaoResult = categoryDao.findBdCategories("", "", FType, null, FNumber, null, null, true, 0, 1, out totalCount);
            if (cateDaoResult.result)
            {
                category = cateDaoResult.resultSet.First<BdCategory>();
            }

            return category;
        }

        /// <summary>
        /// 根据FType和Fstatus获取资料分类列表
        /// </summary>
        /// <param name="FType">状态类型</param>
        /// <param name="Fstatus">状态状态</param>
        /// <author>陈新杰</author>
        /// <returns></returns>
        public List<BdCategory> GetCategoriesByFTypeAndStatus(string FType, string Fstatus)
        {
            List<BdCategory> categories = null;

            int totalCount;
            DAOResult<BdCategory> cateDaoResult = categoryDao.findBdCategories("", "", FType, null, null, null, Fstatus, true, 0, int.MaxValue, out totalCount);
            if (cateDaoResult.result)
            {
                categories = cateDaoResult.resultSet;
            }

            return categories;
        }

        /// <summary>
        /// 根据FType和FNumber获取Category的FId
        /// </summary>
        /// <param name="FType"></param>
        /// <param name="FNumber"></param>
        /// <author>梁建东</author>
        /// <returns></returns>
        public string QueryCategoryFIdByFTypeAndFNumber(String FType, String FNumber)
        {
            return categoryDao.QueryCategoryFIdByFTypeAndFNumber(FType,FNumber);
        }

        /// <summary>
        /// 查找产品单位信息
        /// </summary>
        /// <param name="fName">名称</param>
        /// <param name="fRemark">备注</param>
        /// <author>梁建东</author>
        /// <returns></returns>
        public List<BdCategory> findUnitCategories(String fName, String fRemark, int start, int limit, out int totalCount, string currAccountOrgunitFId)
        {
            IBdOrgunitDao orgDao = new BdOrgunitDaoImpl();
            DAOResult<BdCategory> cateDaoResult = null;
            cateDaoResult = categoryDao.findBdCategories(null, currAccountOrgunitFId, ConstData.CATEGORY_PROD_UNIT_TYPE, fName, null, fRemark, ConstData.CATEGORY_STATUS_YES, true, start, limit, out totalCount);
            if (cateDaoResult.result)
            {
                return cateDaoResult.resultSet;
            }
            else
            {
                throw new Exception(cateDaoResult.msg);
            }
        }

        /// <summary>
        /// 根据Id查找产品单位信息
        /// </summary>
        /// <param name="fId"></param>
        /// <author>梁建东</author>
        /// <returns></returns>
        public BdCategory findUnitCategoryById(String fId)
        {
            DAOResult<BdCategory> cateDaoResult = null;
            int totalCount;
            cateDaoResult = categoryDao.findBdCategories(fId, null, ConstData.CATEGORY_PROD_UNIT_TYPE, null, null, null, ConstData.CATEGORY_STATUS_YES, true, 0, 1, out totalCount);
            if (cateDaoResult.result)
            {
                return cateDaoResult.resultSet.First<BdCategory>();
            }
            else
            {
                throw new Exception(cateDaoResult.msg);
            }
        }

        /// <summary>
        /// 新增或更改产品单位信息
        /// </summary>
        /// <author>梁建东</author>
        /// <param name="fid"></param>
        /// <param name="fName"></param>
        /// <param name="fRemark"></param>
        public void saveOrUpdateUnitCategory(String accountId, string currAccountOrgunitFId, String fId, String fName, String fRemark, out String returnFID)
        {
            returnFID = null;
            BdCategory category = new BdCategory();
            category.FOrgunitId = currAccountOrgunitFId;

            if (fId == null || ConstData.EMPTY_STRING == fId)
            {
                fId = null;
                category.FCreatorId = accountId;
                category.FCreateTime = DateTime.Now;
                category.FModifierId = accountId;
                category.FModifyTime = category.FCreateTime;
            }
            else
            {
                category.FId = fId;
                category.FModifierId = accountId;
                category.FModifyTime = DateTime.Now;
            }
            category.FName = fName;
            category.FNumber = fName;
            category.FRemark = fRemark;
            category.FType = ConstData.CATEGORY_PROD_UNIT_TYPE;
            DAOResult<String> daoResult = categoryDao.saveOrUpdateBdCategory(category);
            if (!daoResult.result)
            {
                throw new Exception(daoResult.msg);
            }
            if (daoResult.resultSet.Count > 0)
            {
                returnFID = daoResult.resultSet.First();
            }

        }
    }
}
